<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="员工id" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入员工id" />
      </el-form-item>
      <el-form-item label="员工工号" prop="userCode">
        <el-input v-model="formData.userCode" placeholder="请输入员工工号" />
      </el-form-item>
      <el-form-item label="员工姓名" prop="userName">
        <el-input v-model="formData.userName" placeholder="请输入员工姓名" />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入部门ID" />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="formData.deptName" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="考勤日期" prop="attendanceDate">
        <el-date-picker
          v-model="formData.attendanceDate"
          type="date"
          value-format="x"
          placeholder="选择考勤日期"
        />
      </el-form-item>
      <el-form-item label="实际上班打卡时间" prop="checkInTime">
        <el-date-picker
          v-model="formData.checkInTime"
          type="date"
          value-format="x"
          placeholder="选择实际上班打卡时间"
        />
      </el-form-item>
      <el-form-item label="标准上班时间" prop="standardCheckInTime">
        <el-date-picker
          v-model="formData.standardCheckInTime"
          type="date"
          value-format="x"
          placeholder="选择标准上班时间"
        />
      </el-form-item>
      <el-form-item label="上班打卡状态(0-正常,1-迟到,2-未打卡,3-外勤打卡)" prop="checkInStatus">
        <el-select v-model="formData.checkInStatus" placeholder="请选择上班打卡状态(0-正常,1-迟到,2-未打卡,3-外勤打卡)">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_CHECK_IN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上班打卡地点" prop="checkInLocation">
        <el-input v-model="formData.checkInLocation" placeholder="请输入上班打卡地点" />
      </el-form-item>
      <el-form-item label="上班打卡设备" prop="checkInDevice">
        <el-input v-model="formData.checkInDevice" placeholder="请输入上班打卡设备" />
      </el-form-item>
      <el-form-item label="实际下班打卡时间" prop="checkOutTime">
        <el-date-picker
          v-model="formData.checkOutTime"
          type="date"
          value-format="x"
          placeholder="选择实际下班打卡时间"
        />
      </el-form-item>
      <el-form-item label="标准下班时间" prop="standardCheckOutTime">
        <el-date-picker
          v-model="formData.standardCheckOutTime"
          type="date"
          value-format="x"
          placeholder="选择标准下班时间"
        />
      </el-form-item>
      <el-form-item label="下班打卡状态(0-正常,1-早退,2-未打卡,3-外勤打卡)" prop="checkOutStatus">
        <el-select v-model="formData.checkOutStatus" placeholder="请选择下班打卡状态(0-正常,1-早退,2-未打卡,3-外勤打卡)">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.OA_CHECK_OUT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="下班打卡地点" prop="checkOutLocation">
        <el-input v-model="formData.checkOutLocation" placeholder="请输入下班打卡地点" />
      </el-form-item>
      <el-form-item label="下班打卡设备" prop="checkOutDevice">
        <el-input v-model="formData.checkOutDevice" placeholder="请输入下班打卡设备" />
      </el-form-item>
      <el-form-item label="实际工作时长(小时)" prop="workHours">
        <el-input v-model="formData.workHours" placeholder="请输入实际工作时长(小时)" />
      </el-form-item>
      <el-form-item label="加班时长(小时)" prop="overtimeHours">
        <el-input v-model="formData.overtimeHours" placeholder="请输入加班时长(小时)" />
      </el-form-item>
      <el-form-item label="迟到分钟数" prop="lateMinutes">
        <el-input v-model="formData.lateMinutes" placeholder="请输入迟到分钟数" />
      </el-form-item>
      <el-form-item label="早退分钟数" prop="leaveEarlyMinutes">
        <el-input v-model="formData.leaveEarlyMinutes" placeholder="请输入早退分钟数" />
      </el-form-item>
      <el-form-item label="是否出差 0.否 1.是" prop="isBusinessTrip">
        <el-radio-group v-model="formData.isBusinessTrip">
          <el-radio
            v-for="dict in getIntDictOptions(DICT_TYPE.INFRA_BOOLEAN_STRING)"
            :key="dict.value"
            :label="dict.value"
          >
            {{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="请假类型(0-无,1-年假,2-病假,3-事假,4-调休)" prop="leaveType">
        <el-select v-model="formData.leaveType" placeholder="请选择请假类型(0-无,1-年假,2-病假,3-事假,4-调休)">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.BPM_OA_LEAVE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="请假时长(小时)" prop="leaveHours">
        <el-input v-model="formData.leaveHours" placeholder="请输入请假时长(小时)" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { AttendanceDetailApi, AttendanceDetailVO } from '@/api/oa/attendancedetail'

/** 员工上下班考勤明细 表单 */
defineOptions({ name: 'AttendanceDetailForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  userCode: undefined,
  userName: undefined,
  deptId: undefined,
  deptName: undefined,
  attendanceDate: undefined,
  checkInTime: undefined,
  standardCheckInTime: undefined,
  checkInStatus: undefined,
  checkInLocation: undefined,
  checkInDevice: undefined,
  checkOutTime: undefined,
  standardCheckOutTime: undefined,
  checkOutStatus: undefined,
  checkOutLocation: undefined,
  checkOutDevice: undefined,
  workHours: undefined,
  overtimeHours: undefined,
  lateMinutes: undefined,
  leaveEarlyMinutes: undefined,
  isBusinessTrip: undefined,
  leaveType: undefined,
  leaveHours: undefined,
})
const formRules = reactive({
  userId: [{ required: true, message: '员工id不能为空', trigger: 'blur' }],
  userCode: [{ required: true, message: '员工工号不能为空', trigger: 'blur' }],
  userName: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' }],
  attendanceDate: [{ required: true, message: '考勤日期不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await AttendanceDetailApi.getAttendanceDetail(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as AttendanceDetailVO
    if (formType.value === 'create') {
      await AttendanceDetailApi.createAttendanceDetail(data)
      message.success(t('common.createSuccess'))
    } else {
      await AttendanceDetailApi.updateAttendanceDetail(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    userCode: undefined,
    userName: undefined,
    deptId: undefined,
    deptName: undefined,
    attendanceDate: undefined,
    checkInTime: undefined,
    standardCheckInTime: undefined,
    checkInStatus: undefined,
    checkInLocation: undefined,
    checkInDevice: undefined,
    checkOutTime: undefined,
    standardCheckOutTime: undefined,
    checkOutStatus: undefined,
    checkOutLocation: undefined,
    checkOutDevice: undefined,
    workHours: undefined,
    overtimeHours: undefined,
    lateMinutes: undefined,
    leaveEarlyMinutes: undefined,
    isBusinessTrip: undefined,
    leaveType: undefined,
    leaveHours: undefined,
  }
  formRef.value?.resetFields()
}
</script>